package com.zl.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zl.model.User;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JDesktopPane desktopPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("图书管理系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 556, 402);
		
		//当前登陆的用户信息
		User currentLoginUser = LoginFrame.loginSuccessUser;
		boolean isAdmin = currentLoginUser.getIsAdmin();
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("图书信息管理");
		mnNewMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/images/base.png")));
		menuBar.add(mnNewMenu);
		
		JMenu menu_4 = new JMenu("按图书类别");
		menu_4.setIcon(new ImageIcon(MainFrame.class.getResource("/images/tushuleibie.png")));
		if (isAdmin) {//是管理员
			mnNewMenu.add(menu_4);
		}
		
		JMenuItem menuItem_7 = new JMenuItem("添加图书类别");
		menuItem_7.setIcon(new ImageIcon(MainFrame.class.getResource("/images/add.png")));
		menuItem_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				AddBookTypeFrame addBookTypeFrame = new AddBookTypeFrame();
				addBookTypeFrame.setVisible(true);
				desktopPane.add(addBookTypeFrame);
			}
		});
		menu_4.add(menuItem_7);
		
		JMenuItem menuItem_8 = new JMenuItem("图书类别维护");
		menuItem_8.setIcon(new ImageIcon(MainFrame.class.getResource("/images/weihu.png")));
		menuItem_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				BookTypeManagerFrame bookTypeManagerFrame = new BookTypeManagerFrame();
				bookTypeManagerFrame.setVisible(true);
				desktopPane.add(bookTypeManagerFrame);
			}
		});
		menu_4.add(menuItem_8);
		
		JMenu menu_5 = new JMenu("按图书名称");
		menu_5.setIcon(new ImageIcon(MainFrame.class.getResource("/images/tushu.png")));
		if (isAdmin) {//是管理员
			mnNewMenu.add(menu_5);
		}
		
		JMenuItem menuItem_9 = new JMenuItem("添加图书");
		menuItem_9.setIcon(new ImageIcon(MainFrame.class.getResource("/images/add.png")));
		menuItem_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				AddBookFrame addBookFrame = new AddBookFrame();
				addBookFrame.setVisible(true);
				desktopPane.add(addBookFrame);
			}
		});
		menu_5.add(menuItem_9);
		
		JMenuItem menuItem_10 = new JMenuItem("图书维护");
		menuItem_10.setIcon(new ImageIcon(MainFrame.class.getResource("/images/weihu.png")));
		menuItem_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				BookManagerFrame bookManagerFrame = new BookManagerFrame();
				bookManagerFrame.setVisible(true);
				desktopPane.add(bookManagerFrame);
			}
		});
		menu_5.add(menuItem_10);
		
		JMenuItem menuItem_6 = new JMenuItem("退出系统");
		menuItem_6.setIcon(new ImageIcon(MainFrame.class.getResource("/images/logout1.png")));
		menuItem_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int result = JOptionPane.showConfirmDialog(null, "是否退出系统?");
				if (result == 0) {
					dispose();
				}
			}
		});
		
		JMenuItem menuItem_11 = new JMenuItem("退出登录");
		menuItem_11.setIcon(new ImageIcon(MainFrame.class.getResource("/images/logout.png")));
		menuItem_11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userLogout(e);
			}
		});
		mnNewMenu.add(menuItem_11);
		mnNewMenu.add(menuItem_6);

		JMenu menu = new JMenu("图书借阅管理");
		menu.setIcon(new ImageIcon(MainFrame.class.getResource("/images/me.png")));
		menuBar.add(menu);
		
		JMenuItem menuItem_3 = new JMenuItem("借阅图书");
		menuItem_3.setIcon(new ImageIcon(MainFrame.class.getResource("/images/jieyueshenqing.png")));
		menuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				BookBorrowFrame bookBorrowFrame = new BookBorrowFrame();
				bookBorrowFrame.setVisible(true);
				desktopPane.add(bookBorrowFrame);
			}
		});
		menu.add(menuItem_3);
		
		JMenuItem menuItem_4 = new JMenuItem("归还图书");
		menuItem_4.setIcon(new ImageIcon(MainFrame.class.getResource("/images/jieyueguihuan.png")));
		menuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				login();
				BookReturnFrame bookReturnFrame = new BookReturnFrame();
				bookReturnFrame.setVisible(true);
				desktopPane.add(bookReturnFrame);
			}
		});
		menu.add(menuItem_4);
		
		JMenuItem menuItem_5 = new JMenuItem("预约图书");
		menuItem_5.setIcon(new ImageIcon(MainFrame.class.getResource("/images/yuyue1.png")));
		menuItem_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				BookAppointmentFrame bookAppointmentFrame = new BookAppointmentFrame();
				bookAppointmentFrame.setVisible(true);
				desktopPane.add(bookAppointmentFrame);
			}
		});
		menu.add(menuItem_5);
		
		JMenu menu_1 = new JMenu("人员管理");
		menu_1.setIcon(new ImageIcon(MainFrame.class.getResource("/images/userName.png")));
		if (isAdmin) {//是管理员

			menuBar.add(menu_1);
		}
		
		JMenuItem menuItem_2 = new JMenuItem("人员信息");
		menuItem_2.setIcon(new ImageIcon(MainFrame.class.getResource("/images/renyuanxinxi.png")));
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				UserInfoFrame userInfoFrame = new UserInfoFrame();
				userInfoFrame.setVisible(true);
				desktopPane.add(userInfoFrame);
			}
		});
		menu_1.add(menuItem_2);
		
		JMenu menu_2 = new JMenu("留言");
		menu_2.setIcon(new ImageIcon(MainFrame.class.getResource("/images/modify.png")));
		menuBar.add(menu_2);
		
		
		JMenuItem menuItem_1 = new JMenuItem("留言板");
		menuItem_1.setIcon(new ImageIcon(MainFrame.class.getResource("/images/liuyanban.png")));
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				MessageBoardFrame messageBoardFrame = new MessageBoardFrame();
				messageBoardFrame.setVisible(true);
				desktopPane.add(messageBoardFrame);
			}
		});
		menu_2.add(menuItem_1);
		
		JMenu menu_3 = new JMenu("关于我们");
		menu_3.setIcon(new ImageIcon(MainFrame.class.getResource("/images/about.png")));
		menuBar.add(menu_3);
		
		JMenuItem menuItem = new JMenuItem("关于开发者");
		menuItem.setIcon(new ImageIcon(MainFrame.class.getResource("/images/guanyuwomen-.png")));
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AboutFrame aboutFrame = new AboutFrame();
				aboutFrame.setVisible(true);
				desktopPane.add(aboutFrame);
			}
		});
		menu_3.add(menuItem);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		desktopPane = new JDesktopPane();
		contentPane.add(desktopPane);
		
		//设置最大化
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}

	/**
	 * 退出登录
	 * @param e
	 */
	private void userLogout(ActionEvent e) {
		// TODO Auto-generated method stub
		int result = JOptionPane.showConfirmDialog(null, "是否退出登录？","提示",JOptionPane.DEFAULT_OPTION);
		
		if (result == 0) {
			dispose();
			LoginFrame.loginSuccessUser = null;
			new LoginFrame().setVisible(true);
		}
	}

	public void login() {
		if (LoginFrame.loginSuccessUser == null) {

			int result = JOptionPane.showConfirmDialog(null, "您未登录，请重新登录","提示",JOptionPane.DEFAULT_OPTION);
	
			if (result == 0) {
				dispose();
				new LoginFrame().setVisible(true);
			}
		}
		
	}
}
